/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package regelinterpreter02;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Patrick
 */
public class Parser {
    private List<Module> modules = new ArrayList<Module>();
    private List<Module> modules2 = new ArrayList<Module>();
    private List<Part> parts = new ArrayList<Part>();
    private List<Part> parts2 = new ArrayList<Part>();
    private List<Rule> rules = new ArrayList<Rule>();
    private List<Rule> rules2 = new ArrayList<Rule>();
    private List<Statement> statements = new ArrayList<Statement>();
    private List<String> lines = new ArrayList<String>();
    private Statement statement = new Statement();
    private Rule rule = new Rule();
    private Part part = new Part();
    private Module module = new Module();
    private String string3 = "";
    private List<ModuleHelp> moduleHelper = new ArrayList<ModuleHelp>();
    private ModuleHelp moduleHelp = new ModuleHelp();
    
    public List<Module> csvParsen(String fileName){
        statements.clear();
        lines.clear();
        try {
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		String zeile = null;
		while ((zeile = in.readLine()) != null) {       //Einlesen der Zeilen der Datei in lines-Liste
			lines.add(zeile);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
        
        int j = 0;
        int k = 0;
        int dim = 0;
        String string = "";
        String string2 = "";
        List<String> stringList = new ArrayList<String>();
        stringList.clear();
        List<String> subStrings = new ArrayList<String>();
        subStrings.clear();
        
        //Statement-Lines in Objekte speichern
        for(String line : lines){
            if(line.startsWith("S")){
                //alle Substrings (durch Semikolon getrennt) in List schreiben
                subStrings.clear();
                for(int i = 2; i < line.length(); i++){
                    if(line.charAt(i) != ';'){
                        string = string + line.charAt(i);
                    } else {
                        subStrings.add(string);
                        string = new String();
                    }
                }
                //Substrings auswerten
                for(int i = 0; i < subStrings.size(); i++){
                    string2 = subStrings.get(i);
                    //Kennung N=Name
                    if(string2.startsWith("N")){
                        string = string2.substring(2, string2.length()-1);
                        statement.setName(string);
                        string = new String();
                    }
                    //Kennung Q=Question
                    if(string2.startsWith("Q")){
                        string = string2.substring(2, string2.length()-1);
                        if(!string.equals("0")) statement.setQuestion(string);
                        string = new String();
                    }
                    //Kennung B=Bild
                    if(string2.startsWith("B")){
                        string = string2.substring(2, string2.length()-1);
                        if(!string.equals("0")) statement.setPicture(string);
                        string = new String();
                    }
                    //Kennung V=Value_string = Werte
                    if(string2.startsWith("V")){
                        string = string2.substring(2, string2.length()-1);
                        dim = Integer.valueOf(string);
                        string = new String();
                        if(dim > 0){
                            for(j = i+1; j < i+1+dim; j++){
                                string = subStrings.get(j).substring(1, subStrings.get(j).length()-1);
                                stringList.add(string);
                                string = new String();
                            }
                            statement.setValue_string(stringList);
                            stringList = new ArrayList<String>();
                            stringList.clear();
                        }
                    }
                    //Kennung D=Diagnosis = Diagnosen zu den Werten
                    if(string2.startsWith("D")){
                        string = string2.substring(2, string2.length()-1);
                        dim = Integer.valueOf(string);
                        string = new String();
                        if(dim > 0){
                            for(j = i+1; j < i+1+dim; j++){
                                string = subStrings.get(j).substring(1, subStrings.get(j).length()-1);
                                stringList.add(string);
                                string = new String();
                            }
                            statement.setDiagnosis(stringList);
                            stringList = new ArrayList<String>();
                            stringList.clear();
                        }
                    }
                }
                statement.setChosenValue(null);
                statements.add(statement);
                statement = new Statement();
                subStrings.clear();
                stringList.clear();
                string = "";
                string2 = "";
                string3 = "";
            }
        }
        
        //Rule-Lines in Objekte speichern
        for(String line : lines){
            if(line.startsWith("R")){
                //alle Substrings (durch Semikolon getrennt) in List schreiben
                subStrings.clear();
                for(int i = 2; i < line.length(); i++){
                    if(line.charAt(i) != ';'){
                        string = string + line.charAt(i);
                    } else {
                        subStrings.add(string);
                        string = new String();
                        string = "";
                    }
                }
                //Substrings auswerten
                for(int i = 0; i < subStrings.size(); i++){
                    string2 = subStrings.get(i);
                    //Kennung N=Name
                    if(string2.startsWith("N")){
                        string = string2.substring(2, string2.length()-1);
                        rule.setName(string);
                        string = new String();
                    }
                    //Kennung T=Type
                    if(string2.startsWith("T")){
                        string = string2.substring(2, string2.length()-1);
                        rule.setType(Integer.valueOf(string));
                        string = new String();
                    }
                    //Kennung I=Integriert
                    if(string2.startsWith("I")){
                        string = string2.substring(2, string2.length()-1);
                        dim = Integer.valueOf(string);
                        string = new String();
                        if(dim > 0){
                            for(j = i+1; j < i+1+dim; j++){
                                string = subStrings.get(j).substring(2, subStrings.get(j).length()-1);
                                stringList.add(string);
                                string = new String();
                            }
                            for(Statement statement2 : statements){ //hier auf eindeutiger Nummer suchen umstellbar
                                for(String string5 : stringList){
                                    if(statement2.getName().equals(string5)) {
                                        rule.setStatement(statement2);
                                    }
                                }
                            }
                            stringList = new ArrayList<String>();
                            stringList.clear();
                        }
                    }
                }
                rules.add(rule);
                rule = new Rule();
                subStrings.clear();
                stringList.clear();
                string = "";
                string2 = "";
                string3 = "";
            }
        }
        
        //Part-Lines in Objekte speichern und Rules zuweisen
        for(String line : lines){
            if(line.startsWith("P")){
                //alle Substrings (durch Semikolon getrennt) in List schreiben
                rules2.clear();
                subStrings.clear();
                for(int i = 2; i < line.length(); i++){
                    if(line.charAt(i) != ';'){
                        string = string + line.charAt(i);
                    } else {
                        subStrings.add(string);
                        string = new String();
                        string = "";
                    }
                }
                //Substrings auswerten
                for(int i = 0; i < subStrings.size(); i++){
                    string2 = subStrings.get(i);
                    //Kennung N=Name
                    if(string2.startsWith("N")){
                        string = string2.substring(2, string2.length()-1);
                        part.setName(string);
                        string = new String();
                    }
                    //Kennung B=Bild
                    if(string2.startsWith("B")){
                        string = string2.substring(2, string2.length()-1);
                        if(!string.equals("0")) part.setPicture(string);
                        string = new String();
                    }
                    //Kennung A=Asset-ID
                    if(string2.startsWith("A")){
                        string = string2.substring(2, string2.length()-1);
                        if(!string.equals("0")) part.setAsset_id(string);
                        string = new String();
                    }
                     //Kennung I=Integriert
                    if(string2.startsWith("I")){
                        string = string2.substring(2, string2.length()-1);
                        dim = Integer.valueOf(string);
                        string = new String();
                        if(dim > 0){
                            for(j = i+1; j < i+1+dim; j++){
                                string3 = subStrings.get(j);
                                if(string3.startsWith("R")){
                                    string = string3.substring(2, string3.length()-1);
                                    stringList.add(string);
                                    string = new String();
                                }
                            }
                            rules2.clear();
                            for(Rule rule2 : rules){ //hier auf eindeutiger Nummer suchen umstellbar
                                for(String string6 : stringList){
                                    if(rule2.getName().equals(string6)) rules2.add(rule2);
                                }
                            }
                            part.setRules(rules2);
                            rules2 = new ArrayList<Rule>();
                            stringList = new ArrayList<String>();
                            stringList.clear();
                        }
                    }
                }
                parts.add(part);
                part = new Part();
                subStrings.clear();
                stringList.clear();
                string = "";
                string2 = "";
                string3 = "";
            }
        }
        
        //Module-Lines in Objekte speichern und Parts zuweisen
        for(String line : lines){
            if(line.startsWith("M")){
                //alle Substrings (durch Semikolon getrennt) in List schreiben
                rules2.clear();
                parts2.clear();
                modules2.clear();
                subStrings.clear();
                for(int i = 2; i < line.length(); i++){
                    if(line.charAt(i) != ';'){
                        string = string + line.charAt(i);
                    } else {
                        subStrings.add(string);
                        string = new String();
                        string = "";
                    }
                }
                 //Substrings auswerten
                for(int i = 0; i < subStrings.size(); i++){
                    string2 = subStrings.get(i);
                    //Kennung N=Name
                    if(string2.startsWith("N")){
                        string = string2.substring(2, string2.length()-1);
                        module.setName(string);
                        string = new String();
                    }
                    //Kennung B=Bild
                    if(string2.startsWith("B")){
                        string = string2.substring(2, string2.length()-1);
                        if(!string.equals("0")) module.setPicture(string);
                        string = new String();
                    }
                    //Kennung A=Asset-ID
                    if(string2.startsWith("A")){
                        string = string2.substring(2, string2.length()-1);
                        if(!string.equals("0")) module.setAsset_id(string);
                        string = new String();
                    }
                     //Kennung I=Integriert
                    if(string2.startsWith("I")){
                        string = string2.substring(2, string2.length()-1);
                        dim = Integer.valueOf(string);
                        string = new String();
                        if(dim > 0){
                        //Rules filtern und dem Module zuweisen
                            for(j = i+1; j < i+1+dim; j++){
                                string3 = subStrings.get(j);
                                if(string3.startsWith("R")){
                                     string = string3.substring(2, string3.length()-1);
                                     stringList.add(string);
                                     string = new String();
                                }
                            }
                            rules2.clear();
                            for(Rule rule2 : rules){ //hier auf eindeutiger Nummer suchen umstellbar
                                for(String string9 : stringList){
                                    if(rule2.getName().equals(string9)) rules2.add(rule2);
                                }
                            }
                            module.setRules(rules2);
                            rules2 = new ArrayList<Rule>();
                            rules2.clear();
                            stringList = new ArrayList<String>();
                            stringList.clear();
                            
                        //Parts filtern und dem Module zuweisen
                            for(j = i+1; j < i+1+dim; j++){
                                string3 = subStrings.get(j);
                                if(string3.startsWith("P")){
                                     string = string3.substring(2, string3.length()-1);
                                     stringList.add(string);
                                     string = new String();
                                }
                            }
                            parts2.clear();
                            
                            for(Part part2 : parts){ //hier auf eindeutiger Nummer suchen umstellbar
                                for(String string4 : stringList){
                                    if(part2.getName().equals(string4)){
                                        parts2.add(part2);
                                    }
                                    
                                }
                            }
                            module.setConsistsOfParts(parts2);
                            parts2 = new ArrayList<Part>();
                            parts2.clear();
                            stringList = new ArrayList<String>();
                            stringList.clear();
                            
                        //Module filtern und dem Module zuweisen
                            moduleHelp.setModule(module);
                            for(j = i+1; j < i+1+dim; j++){
                                string3 = subStrings.get(j);
                                if(string3.startsWith("M")){
                                     string = string3.substring(2, string3.length()-1);
                                     moduleHelp.addStringToConsistsOfModules(string);
                                     string = new String();
                                }
                            }
                            moduleHelper.add(moduleHelp);
                            moduleHelp = new ModuleHelp();
                            stringList = new ArrayList<String>();
                            stringList.clear();
                        }
                    }
                }
                modules.add(module);
                module = new Module();
                subStrings.clear();
                stringList.clear();
                string = "";
                string2 = "";
                string3 = "";
            }
        }
        
        List<Module> modsTmp = new ArrayList<Module>();
        modsTmp.clear();
        
        //Modules filtern und dem Module zuweisen
        for(ModuleHelp moduleHelp2 : moduleHelper){                                 //zuerst richtiges Module finden...
            for(Module module2 : modules){
                if(moduleHelp2.getModule().getName().equals(module2.getName())){
                    for(String moduleName : moduleHelp2.getConsistsOfModules()){    //dann Modules zu den gespeierrten ModuleNames finden...
                        for(Module module3 : modules){
                            if(moduleName.equals(module3.getName())){
                                module2.getConsistsOfModules().add(module3);        //und gefundene Modules dem Module zuweisen
                                modsTmp.add(module3);
                            }
                        }
                    }
                }
            }
        }
        
        for(Module modTmp : modsTmp){
            modules.remove(modTmp);
        }
        
        return modules;
    }
    
}
